package cn.iocoder.yudao.module.carbon.controller.admin.gwp;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import cn.iocoder.yudao.module.carbon.controller.admin.gwp.vo.*;
import cn.iocoder.yudao.module.carbon.convert.gwp.GwpConvert;
import cn.iocoder.yudao.module.carbon.dal.dataobject.gwp.GwpDO;
import cn.iocoder.yudao.module.carbon.service.gwp.GwpService;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;

@Tag(name =  "管理后台 - 全球变暖潜能值")
@RestController
@RequestMapping("/carbon/gwp")
@Validated
public class GwpController {

    @Resource
    private GwpService gwpService;

    @PostMapping("/create")
    @Operation(summary = "创建全球变暖潜能值")
    @PreAuthorize("@ss.hasPermission('carbon:gwp:create')")
    public CommonResult<Long> createGwp(@Valid @RequestBody GwpCreateReqVO createReqVO) {
        return success(gwpService.createGwp(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新全球变暖潜能值")
    @PreAuthorize("@ss.hasPermission('carbon:gwp:update')")
    public CommonResult<Boolean> updateGwp(@Valid @RequestBody GwpUpdateReqVO updateReqVO) {
        gwpService.updateGwp(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除全球变暖潜能值")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('carbon:gwp:delete')")
    public CommonResult<Boolean> deleteGwp(@RequestParam("id") Long id) {
        gwpService.deleteGwp(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得全球变暖潜能值")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('carbon:gwp:query')")
    public CommonResult<GwpRespVO> getGwp(@RequestParam("id") Long id) {
        GwpDO gwp = gwpService.getGwp(id);
        return success(GwpConvert.INSTANCE.convert(gwp));
    }

    @GetMapping("/page")
    @Operation(summary = "获得全球变暖潜能值分页")
    @PreAuthorize("@ss.hasPermission('carbon:gwp:query')")
    public CommonResult<PageResult<GwpRespVO>> getGwpPage(@Valid GwpPageReqVO pageVO) {
        PageResult<GwpDO> pageResult = gwpService.getGwpPage(pageVO);
        return success(GwpConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出全球变暖潜能值 Excel")
    @PreAuthorize("@ss.hasPermission('carbon:gwp:export')")
    @OperateLog(type = EXPORT)
    public void exportGwpExcel(@Valid GwpExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<GwpDO> list = gwpService.getGwpList(exportReqVO);
        // 导出 Excel
        List<GwpExcelVO> datas = GwpConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "全球变暖潜能值.xls", "数据", GwpExcelVO.class, datas);
    }

}
